package aod.android.com.bernay.mine.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.io.IOException;

import aod.android.com.bernay.R;
import aod.android.com.bernay.http.BernayApi;
import aod.android.com.bernay.http.BernayCallback;
import aod.android.com.bernay.mine.activity.DynamicActivity;
import aod.android.com.bernay.mine.module.DynamicBean;
import aod.android.com.bernay.util.SharePreUtils;
import aod.android.com.bernay.util.Utils;
import aod.android.com.bernay.view.activity.PhotoInfoActivity;
import aod.android.com.bernay.view.activity.VideoInfoActivity;
import aod.android.com.bernay.view.decoration.SpacesItemDecoration;

/**
 * @author crated by zhixianuanyang on 2019/4/9 19:49
 * @Email jamesKing0812@gmail.com
 */
public class IamgeViewHolder extends RecyclerView.ViewHolder {

    private final Context mContext;
    private GridLayoutManager layoutManager; //grid布局管理器
    private ImageView ivStatus; //图片状态
    private TextView mStatus;   //文本状态
    private TextView mCreatTime;//创建时间
    private TextView mContent;  //文本内容
    private RecyclerView mRecyclerView; //子布局列表
    private LinearLayout layoutFabulous;//点赞布局
    private TextView mFabulous; //点赞文本
    private ImageView mImaegFabulous;//点赞图片
    private ImageView mImaegComment;//评论图片
    private LinearLayout layoutComment;//评论布局
    private TextView mComment; //评论文本
    private CommonAdapter<String> mAdapter; //适配器
    private String userId; //userid
    private String token;   //token


    public IamgeViewHolder(View view, Context mContext) {
        super(view);
        this.mContext = mContext;
        ivStatus = view.findViewById(R.id.iv_status);
        mStatus = view.findViewById(R.id.tv_status);
        mCreatTime = view.findViewById(R.id.tv_creatTime);
        mContent = view.findViewById(R.id.tv_content);
        mRecyclerView = view.findViewById(R.id.imageRecyclerView);
        layoutFabulous = view.findViewById(R.id.ll_fabulous);
        mFabulous = view.findViewById(R.id.tv_fabulous);
     //   mImaegFabulous = view.findViewById(R.id.iv_fabulous);
        layoutComment = view.findViewById(R.id.ll_comment);
        mComment = view.findViewById(R.id.tv_comment);
     //   mImaegComment = view.findViewById(R.id.iv_comment);
        int leftRight = Utils.dip2px(mContext,15);
        int topBottom = Utils.dip2px(mContext,15);
        mRecyclerView.addItemDecoration(new SpacesItemDecoration(leftRight, topBottom));
    }


    //绑定数据
    public void bindData(final DynamicBean bean) {
        userId = SharePreUtils.newInstance(mContext).getUserId();
        token = SharePreUtils.newInstance(mContext).getToken();

        if (bean.getAuditStatus().equals("1")){
            ivStatus.setVisibility(View.GONE);
            mStatus.setText("审核中");
        }else{
            ivStatus.setVisibility(View.VISIBLE);
            mStatus.setText("已审核");
        }
        mCreatTime.setText(bean.getReleaseTime());
        mContent.setText(bean.getTextContent());
        //点赞
        layoutFabulous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BernayApi.http().postLove(userId, token, "123", new BernayCallback() {
                    @Override
                    public void onJsonData(String json) {
                    }
                    @Override
                    public void onHttpFailure(IOException e) {

                    }
                });
            }
        });

        //评论
        layoutComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BernayApi.http().postLove("123", "123", "123", new BernayCallback() {
                    @Override
                    public void onJsonData(String json) {
                    }
                    @Override
                    public void onHttpFailure(IOException e) {

                    }
                });
            }
        });

        //给图片布局增加样式
        layoutManager = new GridLayoutManager(mContext, 3);
        mRecyclerView.setLayoutManager(layoutManager);

        mAdapter = new CommonAdapter<String>(mContext,R.layout.dynamic_image_item,bean.getPictureArray()) {
            @Override
            protected void convert(ViewHolder holder, String s, int position) {
                //加载图片
                Glide.with(mContext)
                        .load(s.toString())
                        .into((ImageView) holder.getView(R.id.iv_album));
            }
        };
        mRecyclerView.setAdapter(mAdapter);
    }
}
